
/************************************************************************/
/*     	File Name:	TableS1S2.do	     								*/
/*     	Date:   	June 2025   			            				*/
/*      Author: 	Juan Acevedo-Ossa		                        	*/
/*      Purpose:   Replication file for "The Indirect Effects of	    */
/*				   Structural Power										*/
/*      Input Files:  gvcnousnoch.csv, gvconlyus.csv gvconlych.csv		*/	
/*                    panel_unga.csv                                	*/	
/*      Output File:   Table S1 and Table S2                            */
/*      Total Processing time:  4 hrs 10 minutes                        */
/*      Machine:  16.0 GB RAM - Intel Core i7 @ 2.50 GHz 16 cores       */
/************************************************************************/

*Open a log file: the values of F-test will can be accessed inside the log file once the estimation is completed 
log using tableS1.txt, text 
timer clear
timer on 1

**************************************
*RUN PROGRAM FIRST                    *
**************************************

drop _all

pr drop _all

set more off

spmatrix clear

macro drop _all

capture matrix drop _all




program define splag_ll



if "$estimation"== "sar" & "$effect"== "re" {

	args lnf mu rho1 rho2 rho3 sigma
	tempvar A rSL1 rSL2 rSL3 /* rSL4 */
	gen double `rSL1'=`rho1'*SL11
	gen double `rSL2'=`rho2'*SL21
	gen double `rSL3'=`rho3'*SL31

	scalar p1 = `rho1'
	scalar p2 = `rho2'
	scalar p3 = `rho3'

	matrix p1W1 = p1*W1
	matrix p2W2 = p2*W2
	matrix p3W3 = p3*W3

	matrix IpW = I_n - p1W1 - p2W2- p3W3

	qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
	scalar A = `A'
	qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`mu', 0, `sigma'))


}

if "$estimation"== "sarar" & "$effect"== "re"  {

    args lnf mu rho1 rho2 rho3 lambda sigma

    tempvar A rSL1 rSL2 rSL3 rSE

    * Define spatially lagged dependent variables
    gen double `rSL1' = `rho1' * SL11
    gen double `rSL2' = `rho2' * SL21
    gen double `rSL3' = `rho3' * SL31

    * Use precomputed SE instead of matrix multiplication
    gen double `rSE' = `lambda' * SE1  // Now uses the variable SE

    * Define spatial lag matrices
    scalar p1 = `rho1'
    scalar p2 = `rho2'
    scalar p3 = `rho3'
    scalar lam = `lambda'

    matrix p1W1 = p1 * W1
    matrix p2W2 = p2 * W2
    matrix p3W3 = p3 * W3
    matrix lamW4 = lam * W4  // Spatial error matrix

    * Compute determinant adjustment for log-likelihood
    matrix IpW = I_n - p1W1 - p2W2 - p3W3
    matrix IpW_err = I_n - lamW4

    qui gen double `A' = ln(det(IpW)) + ln(det(IpW_err)) / $nobs if _n == 1
    scalar A = `A'

    * Compute log-likelihood
    qui replace `lnf' = A + ln(normalden($ML_y1 - `rSL1' - `rSL2' - `rSL3' - `rSE' - `mu', 0, `sigma'))
	
}


if "$estimation"== "sar" & "$effect"== "fe" {

	args lnf mu rho1 rho2 rho3 sigma
	tempvar A rSL1 rSL2 rSL3 /* rSL4 */
	gen double `rSL1'=`rho1'*SL11_fe
	gen double `rSL2'=`rho2'*SL21_fe
	gen double `rSL3'=`rho3'*SL31_fe

	scalar p1 = `rho1'
	scalar p2 = `rho2'
	scalar p3 = `rho3'

	matrix p1W1 = p1*W1
	matrix p2W2 = p2*W2
	matrix p3W3 = p3*W3

	matrix IpW = I_n - p1W1 - p2W2- p3W3

	qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
	scalar A = `A'
	qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`mu', 0, `sigma'))


}


if "$estimation"== "sarar" & "$effect"== "fe"  {

    args lnf mu rho1 rho2 rho3 lambda sigma

    tempvar A rSL1 rSL2 rSL3 rSE

    * Define spatially lagged dependent variables
    gen double `rSL1' = `rho1' * SL11_fe
    gen double `rSL2' = `rho2' * SL21_fe
    gen double `rSL3' = `rho3' * SL31_fe

    * Use precomputed SE instead of matrix multiplication
    gen double `rSE' = `lambda' * SE1_fe  // Now uses the variable SE

    * Define spatial lag matrices
    scalar p1 = `rho1'
    scalar p2 = `rho2'
    scalar p3 = `rho3'
    scalar lam = `lambda'

    matrix p1W1 = p1 * W1
    matrix p2W2 = p2 * W2
    matrix p3W3 = p3 * W3
    matrix lamW4 = lam * W4  // Spatial error matrix

    * Compute determinant adjustment for log-likelihood
    matrix IpW = I_n - p1W1 - p2W2 - p3W3
    matrix IpW_err = I_n - lamW4

    qui gen double `A' = ln(det(IpW)) + ln(det(IpW_err)) / $nobs if _n == 1
    scalar A = `A'

    * Compute log-likelihood
    qui replace `lnf' = A + ln(normalden($ML_y1 - `rSL1' - `rSL2' - `rSL3' - `rSE' - `mu', 0, `sigma'))
	
}


end




**************************************
*SELECT VARS                     *
**************************************



global Y arms_dist
global X larms_dist sanct_stock ltradegdpuslog laidusgdplog polyarchy pop_log loggdppercapita corruption_control imf atop right_wing dip_visits_usa 

global time year
global unit country_code


forvalues i=1/4 {
local i = `i' 
    if `i' == 1 {
        global effect "re"
        global estimation "sar"
    }
    if `i' == 2 {
        global effect "fe"
        global estimation "sar"
    }  
    if `i' == 3 {
        global effect "re"
        global estimation "sarar"
    }  
    if `i' == 4 {
        global effect "fe"
        global estimation "sarar"
    }  




**************************************
*LOAD MATRICES                       *
**************************************


cd "`c(current_do)'" 

*All countries but China and the US
import delimited "Data\GVC matrices\gvcnousnoch2001.csv", varnames(nonames) clear
qui sum v1
global nobs = r(N)
mkmat v1-v$nobs, matrix(W1)
matrix I_n = I($nobs)

*Only the US
drop _all
import delimited "Data\GVC matrices\gvconlyus2001.csv", clear
mkmat v1-v$nobs, matrix(W2)

*Only China
drop _all
import delimited "Data\GVC matrices\gvconlych2001.csv", clear
mkmat v1-v$nobs, matrix(W3)


* Define a spatial weights matrix for the error term
import delimited "Data\GVC matrices\gvcall2001.csv", clear
mkmat v1-v$nobs, matrix(W4)  // W4 represents spatial diffusion of errors

**************************************
*LOAD DATASET                      *
**************************************
drop _all
import delimited "Data\panel_unga.csv", clear
drop if year==2000

egen country_code = group(countrycodes), label
sort year countrycodes


**************************************
*CREATE LAG SPATIAL VARIABLES        *
**************************************

    * Create Y-variable as a matrix (vector)
    mkmat $Y, matrix(Y)

    * Create 1st spatial-lag in matrix and then save as variable also
    matrix SL1 = W1*Y
    svmat SL1, n(SL1)

    * Create 2nd spatial-lag in matrix and then save as variable also
    matrix SL2 = W2*Y
    svmat SL2, n(SL2)

    * Create 3rd spatial-lag in matrix and then save as variable also
    matrix SL3 = W3*Y
    svmat SL3, n(SL3)
	
    * Run OLS to get initial residuals **after accounting for spatial    lags   of Y**
    qui regress $Y $X SL1 SL2 SL3
    predict res_adjusted, residuals
	
	mkmat res_adjusted, matrix(ResMat)

    * Create the spatially lagged error term
	matrix SE = W4 * ResMat
	svmat SE, n(SE)
	
	
	
	
**************************************
*      APPLY HIGH DIMENSIONAL fe       *
**************************************

		global SL SL11 SL21 SL31 SE1
		

		* Step 1: Compute Means for Fixed Effects
bysort $unit ($time): egen Y_mean = mean($Y)
global X_fe ""  
foreach var in $X {
    bysort $unit ($time): egen `var'_mean = mean(`var')
}

* Step 2: Compute the **Correct** Demeaned Variables (Matching `xtreg, fe`)
gen Y_fe = $Y - Y_mean
global Y_fe "Y_fe"

foreach var in $X {
    gen `var'_fe = `var' - `var'_mean
    global X_fe "$X_fe `var'_fe"
}

* Step 3: Special Handling for Spatial Lags (`SL`)
* (Keep SL structure intact while ensuring fixed effects are removed properly)
global SL_fe "$SL"  // Initially keep SL unchanged

foreach var in $SL {
    bysort $unit ($time): egen `var'_mean = mean(`var')
    gen `var'_fe = `var' - `var'_mean
    global SL_fe "$SL_fe `var'_fe"

}



* Step 1: Create an empty macro for the new SL_fe
global SL_fe_new ""

* Step 2: Loop through SL_fe and remove SL variables
foreach var in $SL_fe {
    if !strpos(" $SL ", "`var'") {
        global SL_fe_new "$SL_fe_new `var'"
    }
}

* Step 3: Overwrite SL_fe with the new macro
global SL_fe "$SL_fe_new"


regress $Y_fe $X_fe SL11_fe SL21_fe SL31_fe



                                                      


if "$estimation"== "sar" & "$effect"== "re" {

**************************************
    * estimation 1 *
    **************************************

  
    * PRODUCE STARTING VALUES VIA OLS
    qui regress $Y $X
    global nobs=e(N)
    matrix OLSb=e(b)
    global OLSsigma=e(rmse)

    * ESTIMATE SPATIAL LAG estimation
    * Designate estimation to estimate
    ml model lf splag_ll (mu: $Y=$X) (rho1:) (rho2:) (rho3:) (sigma:) /*(rho4:)*/

    * Specify starting values
    ml init OLSb
    ml init rho1:_cons=0
    ml init rho2:_cons=0
    ml init rho3:_cons=0
    * ml init rho4:_cons=0
    ml init sigma:_cons=$OLSsigma
    * ml max, difficult trace
    ml max, trace
	estimates store SAR
	estat ic

}

if "$estimation"== "sarar" & "$effect"== "re" {


    **************************************
    * MODEL 2 *
    **************************************

    * PRODUCE STARTING VALUES VIA OLS
    qui regress $Y $X 
	
    global nobs=e(N)
    matrix OLSb=e(b)
    global OLSsigma=e(rmse)

    * ESTIMATE SPATIAL LAG MODEL
    * Designate model to estimate
	ml model lf splag_ll (mu: $Y=$X) (rho1:) (rho2:) (rho3:) (lambda:) (sigma:)

* Specify starting values
	ml init OLSb
	ml init rho1:_cons=0
	ml init rho2:_cons=0
	ml init rho3:_cons=0
	ml init lambda:_cons=0
	ml init sigma:_cons=$OLSsigma

	ml max, trace
	estimates store SARAR
	estat ic

}






if "$estimation"== "sar" & "$effect"== "fe" {

**************************************
    * estimation 1 *
    **************************************

    * PRODUCE STARTING VALUES VIA OLS
    qui regress $Y_fe $X_fe
    global nobs=e(N)
    matrix OLSb=e(b)
    global OLSsigma=e(rmse)

    * ESTIMATE SPATIAL LAG estimation
    * Designate estimation to estimate
    ml model lf splag_ll (mu: $Y_fe=$X_fe) (rho1:) (rho2:) (rho3:) (sigma:) /*(rho4:)*/

    * Specify starting values
    ml init OLSb
    ml init rho1:_cons=0
    ml init rho2:_cons=0
    ml init rho3:_cons=0
    * ml init rho4:_cons=0
    ml init sigma:_cons=$OLSsigma
    * ml max, difficult trace
    ml max, trace
	estimates store SARFE
	estat ic
	


}

if "$estimation"== "sarar" & "$effect"== "fe" {

**************************************
    * estimation 1 *
    **************************************

    * PRODUCE STARTING VALUES VIA OLS
    qui regress $Y_fe $X_fe
    global nobs=e(N)
    matrix OLSb=e(b)
    global OLSsigma=e(rmse)

    * ESTIMATE SPATIAL LAG estimation
    * Designate estimation to estimate
	ml model lf splag_ll (mu: $Y_fe=$X_fe) (rho1:) (rho2:) (rho3:) (lambda:) (sigma:)

* Specify starting values
	ml init OLSb
	ml init rho1:_cons=0
	ml init rho2:_cons=0
	ml init rho3:_cons=0
	ml init lambda:_cons=0
	ml init sigma:_cons=$OLSsigma

	ml max, trace
	estimates store SARARFE
	estat ic
	
	

}


	
	
}

timer off 1
timer list
log close


/************************************************************************/
/*     	TABLE S1					     								*/
/************************************************************************/



	
	* Step 4: Run esttab with dynamic renaming
esttab SAR SARFE SARAR SARARFE, ///
	mgroups("SAR" "SAR" "SAR+Error" "SAR+Error", pattern(1 1 1 1)) /// 
    mtitles("RE" "FE" "RE" "FE") ///
    cells("b(star fmt(4))" se(par fmt(4))) ///
    modelwidth(10) nodepvars ///
    varlabels(larms_dist "Arms alignment Y(t-1)" ///
              polyarchy "Democracy" ///
              pop_log "log(Population)" ///
              loggdppercapita "log(GDP per capita)" ///
              corruption_control "Control of corruption" ///
              imf "IMF commitment" ///
              right_wing "Right Wing" ///
			  laidusgdplog "log(USAID/GDP)" ///
              sanct_stock "Sanctions" ///
              ltradegdpuslog "TradeUS/GDPlog (t-1)" ///
              atop "Alliances" ///
              dip_visits_usa "Diplomatic") ///
    order(larms_dist polyarchy pop_log loggdppercapita corruption_control ///
          imf right_wing laidusgdplog sanct_stock ltradegdpuslog ///
           atop dip_visits_usa) ///
    stats(N aic bic ll, fmt(%9.3f) label("Observations" "AIC" "BIC" "LL")) ///
    title("mSTAR models of vote coincidence") ///
    addnotes("* p-value < 0.10; ** p-value < 0.05 level; *** p-value < 0.01 level.")
	
	
timer off 1
timer list
	
	
/************************************************************************/
/*     	               TABLE S2    F-TESTS								*/
/************************************************************************/
	
	
estimates restore SAR
 test [rho1]_cons = [rho2]_cons
    test [rho1]_cons = [rho3]_cons
    test [rho2]_cons = [rho3]_cons


estimates restore SARFE
 test [rho1]_cons = [rho2]_cons
    test [rho1]_cons = [rho3]_cons
    test [rho2]_cons = [rho3]_cons
	
	
estimates restore SARAR
 test [rho1]_cons = [rho2]_cons
    test [rho1]_cons = [rho3]_cons
    test [rho2]_cons = [rho3]_cons


estimates restore SARARFE
 test [rho1]_cons = [rho2]_cons
    test [rho1]_cons = [rho3]_cons
    test [rho2]_cons = [rho3]_cons



	
	
	